<!--
 * @Author: ZhaoZhiqi
 * @Date: 2024-10-08 14:05:54
 * @LastEditors: ZhaoZhiqi
 * @LastEditTime: 2024-10-16 17:30:11
 * @Description: 
 * @FilePath: \charging-mini\充电项目\pages\feedback.vue
-->
<template>
    <div class="container">
        <div class="card">
            <div class="card-title">反馈信息</div>
            <div class="card-content">
                <div class="item">
                    <div class="label">充电记录id：</div>
                    <div class="value">{{ charge_order_no }}</div>
                </div>
                <div class="item">
                    <div class="label">设备序列号：</div>
                    <div class="value">{{ device_serial_num }}</div>
                </div>
            </div>
        </div>
        <div class="card">
            <div class="card-title">反馈类型</div>
            <div class="card-content">
                <!-- 反馈类型 -->
                <div class="tags">
                    <div class="tag" :class="{ selected: tagIndex === index }" v-for="(item, index) in typeList" :key="item.value" @click="tagIndex = index">{{ item.label }}</div>
                </div>
            </div>
        </div>
        <div class="card">
            <div class="card-title">反馈图片</div>
            <div class="card-content">
                <!-- 上传图片 -->
                <ant-uploader :maxCount="1" @change="onChange" :onUpload="onUpload" uploadingText="上传中……" uploadfailedText="上传失败"></ant-uploader>
            </div>
        </div>
        <div class="card">
            <div class="card-title">反馈内容</div>
            <div class="card-content">
                <textarea v-model="content" maxlength="100" placeholder="请输入100字以内您想反馈的问题"> </textarea>
                <span class="content-length">{{ content.length }}/100</span>
            </div>
        </div>

        <div class="submit" @click="submit">提交申请</div>
    </div>
</template>
<script>
import { baseUrl } from "../src/api/baseUrl";
import { submitFeedbackAPI } from "../src/api/api";

const onUpload = (file) => {
    return new Promise((resolve, reject) => {
        let token = uni.getStorageSync("token");
        let url = `${baseUrl()}charging-mini/api/mini/auth/file/upload_image`;
        uni.uploadFile({
            url,
            header: {
                Authorization: token,
            },
            filePath: file.path,
            name: "file",
            success: (res) => {
                let data = JSON.parse(res.data);
                let img = data.data;
                resolve(img);
            },
            fail: (err) => {
                console.log(err);
                reject(err);
            },
        });
    });
};
export default {
    data() {
        return {
            onUpload,
            loading: false,
            typeList: [
                {
                    label: "充电记录",
                    value: "CHARGE_RECORD",
                },
                {
                    label: "设备问题",
                    value: "SITE_STATION",
                },
            ],
            fileList: [],
            tagIndex: 0,
            content: "", // 反馈内容

            charge_order_no: "",
            device_serial_num: "",
        };
    },
    onShareAppMessage() {},
    onLoad(optinos) {
        this.charge_order_no = optinos.charge_order_no || "";
        this.device_serial_num = optinos.device_serial_num || "";
    },
    methods: {
        onChange(fileList) {
            let done = true;
            fileList.detail.forEach((item) => {
                if (item.status !== "done") {
                    done = false;
                }
            });
            if (done) {
                this.fileList = fileList.detail.map((item) => item.url);
                console.log(this.fileList);
            }
        },
        submit() {
            if (this.loading) return false;
            if (!this.content) {
                uni.showToast({
                    title: "请输入反馈内容",
                    icon: "none",
                });
                return false;
            }
            this.loading = true;
            submitFeedbackAPI({
                charge_order_no: this.charge_order_no,
                feedback_type: this.typeList[this.tagIndex].value,
                imgs: this.fileList && this.fileList.length ? this.fileList[0] : "",
                reason_desc: this.content,
                station_serial_num: this.device_serial_num,
            })
                .then((res) => {
                    uni.showToast({
                        title: "提交反馈成功",
                        icon: "success",
                    });
                    this.loading = false;
                    setTimeout(() => {
                        uni.navigateBack();
                    }, 1500);
                })
                .catch((err) => {
                    this.loading = false;
                });
        },
    },
};
</script>
<style scoped lang="scss">
.container {
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: #f6f6f6;
    padding-top: 24rpx;
    min-height: 100vh;
    box-sizing: border-box;
    .card {
        width: 694rpx;
        padding: 28rpx;
        box-sizing: border-box;
        border-radius: 24rpx;
        background-color: #fff;
        margin-bottom: 24rpx;
        .card-title {
            font-size: 32rpx;
            line-height: 42rpx;
            color: #333;
            margin-bottom: 28rpx;
        }
        .card-content {
            position: relative;
            .item {
                display: flex;
                align-items: center;
                justify-content: space-between;
                font-size: 28rpx;
                line-height: 38rpx;
                margin-bottom: 20rpx;
                &:last-child {
                    margin: 0;
                }
                .label {
                    color: #a9a9a9;
                }
                .value {
                    color: #333;
                }
            }
            .tags {
                display: flex;
                .tag {
                    color: #9a9a9a;
                    background-color: #f6f6f6;
                    font-size: 24rpx;
                    line-height: 1;
                    padding: 12rpx 20rpx;
                    border-radius: 8rpx;
                    margin-right: 20rpx;
                    margin-bottom: 24rpx;
                    &.selected {
                        color: #fff;
                        background-color: #08bfa8;
                    }
                }
            }
            textarea {
                width: 100%;
                height: 240rpx;
                background-color: #f6f6f6;
                border-radius: 16rpx;
                padding: 24rpx;
                font-size: 28rpx;
                line-height: 38rpx;
                color: #333;
                box-sizing: border-box;
            }
            .content-length {
                font-size: 24rpx;
                line-height: 1;
                position: absolute;
                right: 24rpx;
                bottom: 24rpx;
            }
        }
    }
    .submit {
        position: fixed;
        left: 28rpx;
        bottom: 80rpx;
        width: 694rpx;
        height: 84rpx;
        border-radius: 42rpx;
        background: linear-gradient(90deg, #8bdab0 0%, #08bfa8 100%);
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 28rpx;
        line-height: 1;
        color: #fff;
    }
}
</style>
